package tuiLayer;
import controlLayer.*;
import modelLayer.*;
import java.util.*;

public class LoanUI
{
    private LoanCtr loanCtr;
    private CustomerCtr customerCtr;
    private ItemCtr itemCtr;

    public LoanUI(){
        loanCtr = new LoanCtr();
        customerCtr = new CustomerCtr();
        itemCtr = new ItemCtr();
    }

    public void start(){
        mainMenuLoan();
    }

    private void mainMenuLoan(){
        boolean stop = false;

        while(!stop){
            int choice = writeMainMenuLoan();

            switch(choice){
                case 0:
                    stop = true;
                    break;
                case 1:
                    createLoan();
                    break;
                case 2:
                    findLoan();
                    break;
                case 3:
                    updateLoanStatus();
                    break;
                case 4:
                    deleteLoan();
                    break;
                default: System.out.println("Enter a valid choice");
            }
        }
    }

    private int writeMainMenuLoan(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Loan menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Create loan");
        System.out.println("(2) Find loan");
        System.out.println("(3) Update loan status");
        System.out.println("(4) Delete loan");
        System.out.println("\nType your choice");
        int choice= keyboard.nextInt();
        return choice;
    }

    private void createLoan(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Loan creation---");
        System.out.println();

        System.out.println("Enter the customer id:");
        int idC = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the date:");
        String date = keyboard.nextLine();

        System.out.println("Enter the period:"); //MEMOOOOOOOO!move to orderlineloan
        String period = keyboard.nextLine();

        loanCtr.createLoan(date,period ,customerCtr.findCustomer(idC));
        addItemToLoanMenu();

        System.out.println("Loan successfully created!");
    }

    private void addItemToLoan(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the barcode of the item:");
        int barcode = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the quantity:");
        int quantity = keyboard.nextInt();
        keyboard.nextLine();

        OrderLineLoan orderLineLoan = new OrderLineLoan(quantity,itemCtr.findItem(barcode));
        loanCtr.addItemToLoan(orderLineLoan);
    }

    private void addItemToLoanMenu(){
        Scanner keyboard = new Scanner(System.in);
        boolean stop = false;
        while(!stop){
            int choice = writeAddItemToLoanMenu();
            switch(choice){
                case 0:
                    stop = true;
                    break;             
                case 1:
                    addItemToLoan();
                    break;
                default : System.out.println("Enter a valid choice");
            }
        }
    }

    private int writeAddItemToLoanMenu(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\n*****Add item to loan menu*****");
        System.out.println("(0) Back");
        System.out.println("(1) Add item and quantity");
        int choice= keyboard.nextInt();
        return choice;
    }

    private void findLoan(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Loan search---");
        System.out.println();

        System.out.println("Enter the id of the loan:");
        int idL = keyboard.nextInt();

        Loan loan = loanCtr.findLoan(idL);
        System.out.println("--------------------------------");
        System.out.println("Customer's Name:\t\t"+loan.getCustomerName());
        System.out.println("Date:\t\t "+loan.getDate());
        System.out.println("Period:\t\t"+loan.getPeriod());
        System.out.println("Status:\t\t"+loan.getStatus());
        System.out.println("Price WITHOUT discount:\t"+loan.getTotalPrice());
        System.out.println("Price WITH discount:\t"+loan.discountPrice());
        System.out.println("--------------------------------");
    }

    private void updateLoanStatus(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Loan status update---");
        System.out.println();

        System.out.println("Enter the loan id:");
        int idL = keyboard.nextInt();
        keyboard.nextLine();

        System.out.println("Enter the loan status:");
        String status = keyboard.nextLine();

        loanCtr.updateLoanStatus(idL,status);
    }

    private void deleteLoan(){
        Scanner keyboard = new Scanner(System.in);
        System.out.println("---Loan removal---");
        System.out.println();

        System.out.println("Enter the loan id:");
        int idL = keyboard.nextInt();
        keyboard.nextLine();

        loanCtr.deleteLoan(idL);
        System.out.println("Loan successfuly deleted!");
    }
}